<?php
/**
 * File: Base.class.php
 * Author: camy <375766145@qq.com>
 * Time: 2018/3/18 18:07
 */

namespace Common\Service;

use Common\Extend\Traits\StaticInstance;
use Think\Model;

class Base
{
    use StaticInstance;

    public $pageSize = 10;
    public $error = '数据存取失败~请稍后再试';

    /**
     * 唯一索引列生成不重复值
     * @param Model $model
     * @param $column
     * @param \Closure $closure
     * @return mixed
     */
    protected function uniqueKey(Model $model, $column, \Closure $closure)
    {
        $value = $closure();
        $res = $model->where([$column => $value])->find();
        if ($res) {
            $value = $this->uniqueKey($model, $column, $closure);
        }
        return $value;
    }

    /**
     * 验证方法
     * @param Model $model
     * @param $rule
     * @param $data
     * @return  bool
     */
    protected function validate(Model $model, $rule, $data)
    {
        $res = $model->validate($rule)->create($data);
        if (!$res) {
            $this->error = $model->getError();
            return false;
        }
        return true;
    }
}